<template>
  <div class="spec-preview">
    <!-- isDefault为1时 显示大图 -->
    <img
      v-lazy="skuInfo.skuImageList[imgDefaultIndex].imgUrl"
      v-if="skuInfo.skuImageList"
    />
    <!-- 大盒子 -->
    <div class="event" @mousemove="zoomImage" ref="maskDiv"></div>
    <!-- 隐藏的大图 -->
    <div class="big" ref="bigImgDiv">
      <img
        v-lazy="skuInfo.skuImageList[imgDefaultIndex].imgUrl"
        v-if="skuInfo.skuImageList"
        ref="bigImg"
      />
    </div>
    <!-- 蒙版 -->
    <div class="mask" ref="mask"></div>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
export default {
  name: "Zoom",
  data() {
    return {
      /* 大图默认下标 */
      imgDefaultIndex: 0,
    };
  },
  mounted() {
    /* 全局事件总线 接收参数 修改图片默认下标标识 实现小图联动大图*/
    this.$bus.$on("changeImgDefaultIndex", (index) => {
      this.imgDefaultIndex = index;
    });
  },
  methods: {
    /* 放大镜 */
    zoomImage(e) {
      let maskDiv = this.$refs.maskDiv; //装蒙版的盒子
      let mask = this.$refs.mask; //蒙版
      let bigImg = this.$refs.bigImg; //大图
      let bigImgDiv = this.$refs.bigImgDiv; //装大图的盒子
      /* 鼠标当前所在坐标 */
      let mouseX = e.offsetX;
      let mouseY = e.offsetY;
      mask.style.pointerEvents = "none"; //防抖动
      /* 蒙版偏移量 = 鼠标坐标-自身宽高的一半 */
      let maskLeft = mouseX - mask.clientWidth / 2;
      let maskTop = mouseY - mask.clientHeight / 2;
      /* 蒙版最大偏移量 = 大盒子宽高-自身宽高 */
      /* 边界效果 */
      if (maskTop < 0) {
        maskTop = 0;
      } else if (maskTop > maskDiv.clientHeight - mask.clientHeight) {
        maskTop = maskDiv.clientHeight - mask.clientHeight;
      }
      if (maskLeft < 0) {
        maskLeft = 0;
      } else if (maskLeft > maskDiv.clientWidth - mask.clientWidth) {
        maskLeft = maskDiv.clientWidth - mask.clientWidth;
      }

      /* 大图偏移量/最大偏移量= 蒙版偏移量/最大偏移量 */
      let bigImgTop =
        (maskTop / (maskDiv.clientHeight - mask.clientHeight)) *
        (bigImg.clientHeight - bigImgDiv.clientHeight);
      let bigImgLeft =
        (maskLeft / (maskDiv.clientWidth - mask.clientWidth)) *
        (bigImg.clientWidth - bigImgDiv.clientWidth);
      /* 蒙版最终偏移量 */
      mask.style.top = maskTop + "px";
      mask.style.left = maskLeft + "px";
      /* 大图最终偏移量 */
      bigImg.style.top = -bigImgTop + "px";
      bigImg.style.left = -bigImgLeft + "px";
    },
  },
  computed: {
    ...mapGetters(["skuInfo"]),
  },
};
</script>

<style lang="less">
.spec-preview {
  position: relative;
  width: 400px;
  height: 400px;
  border: 1px solid #ccc;

  img {
    width: 100%;
    height: 100%;
  }

  .event {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    left: 0;
    z-index: 998;
  }

  .mask {
    width: 50%;
    height: 50%;
    background-color: rgba(0, 255, 0, 0.3);
    position: absolute;
    left: 0;
    top: 0;
    display: none;
  }

  .big {
    width: 100%;
    height: 100%;
    position: absolute;
    top: -1px;
    left: 100%;
    border: 1px solid #aaa;
    overflow: hidden;
    z-index: 998;
    display: none;
    background: white;

    img {
      width: 200%;
      max-width: 200%;
      height: 200%;
      position: absolute;
      left: 0;
      top: 0;
    }
  }

  .event:hover ~ .mask,
  .event:hover ~ .big {
    display: block;
  }
}
</style>